package com.itstyle.seckill.topic.redis.common;


import com.itstyle.seckill.topic.redis.Iservice.Request;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ConcurrentHashMap;


/**
 * @Auther: liuwenxiang
 * @Date: 2020/2/23 18:00
 * @Description:
 */
public class RequestQueue {
    /**
     * 这个全局的ArrayList没有线程安全问题：
     * 1.RequestQueue是单例对象，所有queues也是全局共享对象
     * 2.queues的写入操作只在项目启动的时候会执行，所以没有线程安全问题（只会并发读，不会并发写）
     */
    private List<ArrayBlockingQueue<Request>> queues = new ArrayList<>();

    private Map<Integer, Boolean> flagMap = new ConcurrentHashMap<>();

    private RequestQueue() {

    }

    private static class Singleton {
        private static RequestQueue queue;

        static {
            queue = new RequestQueue();
        }

        public static RequestQueue getInstance() {
            return queue;
        }
    }

    public static RequestQueue getInstance() {
        return Singleton.getInstance();
    }

    public void addQueue(ArrayBlockingQueue<Request> queue) {
        queues.add(queue);
    }

    public int getQueueSize() {
        return queues.size();
    }

    public ArrayBlockingQueue<Request> getQueueByIndex(int index) {
        return queues.get(index);
    }

    public Map<Integer, Boolean> getFlagMap() {
        return this.flagMap;
    }
}

